package com.yanqu.road.dao.impl.activity.dafuweng;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.dafuweng.DFWBuildingConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DFWBuildingConfigDaoImpl extends TempDao {

    private static final String sql = "select * from t_s_monopoly_building where activityId = ?";

    public Map<Integer, DFWBuildingConfig> getDFWBuildingConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DFWBuildingConfig> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWBuildingConfig config = new DFWBuildingConfig();
                config.setBuildingId(rs.getInt("id"));
                config.setBuildingName(rs.getString("name"));
                config.setBuildingDesc(rs.getString("desc"));
                config.setIcon(rs.getString("icon"));
                config.setBaseEvent(rs.getInt("baseEvent"));
                config.setReachEventList(parseEventList(rs.getString("reachEventList")));
                config.setPassEventList(parseEventList(rs.getString("passEventList")));
                config.setPositionList(StringUtils.stringToIntegerList(rs.getString("positionList"), ";"));
                config.setShowSkillId(rs.getInt("skillId"));
                config.setCollectTask(rs.getString("collectTask"));
                config.setCollectRewards(rs.getString("collectRewards"));
                map.put(config.getBuildingId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private List<Map<Integer, Integer>> parseEventList(String param) {
        if (StringUtils.isNullOrEmpty(param) || param.equals("0")) {
            return new ArrayList<>();
        }
        List<Map<Integer, Integer>> list = new ArrayList<>();
        List<String> eventGroupList = StringUtils.stringToStringList(param, "\\|");
        for (String eventGroup : eventGroupList) {
            Map<Integer, Integer> map = new ConcurrentHashMap<>();
            List<String> eventList = StringUtils.stringToStringList(eventGroup, ";");
            for (String event : eventList) {
                List<Integer> eventParam = StringUtils.stringToIntegerList(event, ",");
                map.put(eventParam.get(0), eventParam.get(1));
            }
            list.add(map);
        }
        return list;
    }
}
